// pages/chooseCommunity/chooseCommunity.js
const app = getApp();
import api from '../../../../utils/util.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    navmess:{
      navtitle: "选择小区",
      backhide: 0,
      topheight: 1
    },
    index1: -1,
    index2: -1,
    index3: -1,
    provinceArr: [],
    cityArr: [],
    regionArr: [],
    // 小区列表
    demolist: [],
    pageNum: 1,
    pageSize: 8,
    loading: false,
    noMore: false,
    loadingFailed: false,
  },
  pageto: function(e){
    var url = e.currentTarget.dataset.url,
        data = e.currentTarget.dataset.datobj;
    app.goto(url,data);
  },
  // 省市区picker获取
  bindPickerChange1: function (e) {
    var that = this;
    console.log('省份', e.detail.value)
    this.setData({
      index1: e.detail.value,
      provinceId: this.data.provinceArr[e.detail.value].id,
    })
    api.post("/common/regionList", {
      "pid": that.data.provinceId
    }).then(res => {
      console.log("城市", res)
      that.setData({
        cityArr: res.result,
        index2: 0,
        cityId: res.result[0].id
      })
      that.getArea(that);
    })
  },
  bindPickerChange2: function (e) {
    var that = this;
    console.log('城市', e.detail.value)
    this.setData({
      index2: e.detail.value,
      cityId: this.data.cityArr[e.detail.value].id
    })
    api.post("/common/regionList", {
      "pid": that.data.cityId
    }).then(res => {
      console.log("区域", res)
      that.setData({
        regionArr: res.result,
        index3: 0,
        regionId: res.result[0].id
      })
    })
  },
  bindPickerChange3: function (e) {
    var that = this;
    console.log('区域', e.detail.value)
    this.setData({
      index3: e.detail.value,
      regionId: this.data.regionArr[e.detail.value].id
    })
  },
  checkNull: function(){
    if(!this.data.cityArr.length){
      wx.showToast({
        title: '请选择省份',
        icon: "none",
        duration: 1500
      })
    }
  },
  // serch input
  serchAreaInfo: function(e){
    this.setData({
      areaInfo: e.detail.value
    })
  },
  // 数据请求封装
  getData: function(){
    var ent = app.globalData.ent,
        token = this.data.token,
        provinceId = this.data.provinceId, 
        cityId = this.data.cityId,
        regionId = this.data.regionId,
        areaInfo = this.data.areaInfo,
        pageNum = this.data.pageNum,
        pageSize = this.data.pagesSize;
    console.log(token,provinceId,cityId,regionId,areaInfo);
    if(provinceId&&cityId&&regionId&&areaInfo){
      var parmes = {
        "enterpriseId": ent,
        "token": token,
        "provinceId": provinceId,
        "cityId": cityId,
        "areaId": regionId,
        "name": areaInfo,
        "pagrNum": pageNum,
        "pageSize": pageSize
      }
    }else if(!provinceId&&areaInfo){
      var parmes = {
        "enterpriseId": ent,
        "token": token,
        "name": areaInfo,
        "pagrNum": pageNum,
        "pageSize": pageSize
      }
    }else if(provinceId&&!areaInfo){
      var parmes = {
        "enterpriseId": ent,
        "token": token,
        "provinceId": provinceId,
        "cityId": cityId,
        "areaId": regionId,
        "pagrNum": pageNum,
        "pageSize": pageSize
      }
    }else if(!provinceId&&!areaInfo){
      var parmes = {
        "enterpriseId": ent,
        "token": token,
        "pagrNum": pageNum,
        "pageSize": pageSize
      }
    }
    api.post("/mini/my/basic/smallAreaList",parmes).then(res => {
      console.log("小区列表信息",res)
      this.setData({
        demolist: res.result.list
      })
    })
  },
  // 搜索
  getCommunitylist: function(){
    var that = this;
    this.setData({
      pageNum: 1
    })
    that.getData();
  },
  // 触底分页请求
  scrollToLower: function(e){
    var that = this;
    var pageNum = this.data.pageNum;
    pageNum++;
    that.setData({
      pageNum: pageNum
    })
    that.getData();
  },
  // top下拉刷新 
  //触摸开始
  handletouchstart: function (event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function (event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function (event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function (event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function (event) {
    this.refreshView.onPageScroll(event)
  },
  onPullDownRefresh:function(){
    console.log("-----下拉刷新-----")
    var that = this,
        index = this.data.currentTab;
    setTimeout(() => { this.refreshView.stopPullRefresh()},1500)
  },
  _pullState: function(e){
    // console.log(e.detail) 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
     // 刷新组件
     this.refreshView = this.selectComponent("#refreshView")
    var token = wx.getStorageSync('token');
    this.setData({
      token: token
    })
    // 默认渲染所有小区
    api.post("/mini/my/basic/smallAreaList",{
      "enterpriseId": app.globalData.ent,
      "token": token,
      "pagrNum": 1,
      "pageSize": that.data.pageSize
    }).then(res => {
      console.log("小区列表信息",res)
      this.setData({
        demolist: res.result.list
      })
    })
    // 取省份
    api.post("/common/regionList",{
      "pid": 100000
    }).then(res => {
      console.log("省份",res)
      that.setData({
        provinceArr: res.result
      })
    })
  },
  getCity: function (self) {
    api.post("/common/regionList", {
      "pid": self.data.provinceId
    }).then(res => {
      console.log("城市", res)
      self.setData({
        cityArr: res.result,
        index2: 0,
        cityId: res.result[0].id
      })
      self.getArea(self);
    })
  },
  getArea: function (other) {
    api.post("/common/regionList", {
      "pid": other.data.cityId
    }).then(res => {
      console.log("区域", res)
      other.setData({
        regionArr: res.result,
        index3: 0,
        regionId: res.result[0].id
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})